<?php

class edit_honour_cat
{
	var $id;
	var $title;
	var $cat;
	var $division;
	var $order;
	
	var $add_record;

	function edit_honour_cat($add_record, $division, $record='')
	{
		if($add_record)
		{
			$this->add_record = true;
			$this->id = '';
			$this->cat = 0;
			$this->division = $division;
			$this->order = '';
		}
		else
		{
			$this->add_record = false;
			$this->id = $record['hc_id'];
			$this->title = $record['hc_title'];
			$this->cat = $record['hc_cat'];
			$this->division = $division;
			$this->order = $record['hc_order'];
		}
	}

	function render()
	{		
		global $skin_dir, $lang;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['EditButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'editRecord';
		$remS = $this->add_record ? '<!--' : '';
		$remE = $this->add_record ? '-->' : '';
		
		$id = $this->id;
		$division = $this->division;
		
		$title = $this->title;
		$order = $this->order;
		
		$cat_options = '<option value="0" ' . ($this->cat == 0 ? 'SELECTED' : '') . '>' . $lang['Drivers'] . '</option>' .
					   '<option value="1" ' . ($this->cat == 1 ? 'SELECTED' : '') . '>' . $lang['Teams'] . '</option>' .
					   '<option value="2" ' . ($this->cat == 2 ? 'SELECTED' : '') . '>' . $lang['Manufacturers'] . '</option>';
		
		include $skin_dir . 'admin/edithonourcat.tpl';
	}
}


class honour_cat
{
	var $mess;
	var $division;
	var $toRender = Array();

	function honour_cat($division)
	{
		$mess = '';
		$this->division = $division;
		if(isset($_POST['removeRecord']) && isset($_POST['id']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		//get the record
		$qSel = "select hc_title from ". HONOURCATS_TABLE . " Where hc_id = " . $_POST['id'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDelEnt = "delete from ". HONOURENTRIES_TABLE . " Where he_cat = " . $_POST['id'];
			$qDel = "delete from ". HONOURCATS_TABLE . " Where hc_id = " . $_POST['id'];
			
			$db->sql_query($qDel);
			$db->sql_query($qDelEnt);
			$this->mess = new addMessage(str_replace('{entry}', $row['hc_title'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". HONOURCATS_TABLE . " (hc_title, hc_cat, hc_division, hc_order) values('" .
							 $_POST['title'] . "', " . $_POST['category'] .", " . $this->division . ", " . $_POST['order'] .')';
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". HONOURCATS_TABLE . " set hc_title = '" . $_POST['title'] .
													  "', hc_cat = " . $_POST['category'] .
													  ", hc_order = " . $_POST['order']  .
													  " WHERE hc_id = " . $_POST['id'] ;
			
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
			
		}
		
		
		return $valid && is_numeric($_POST['order'])
 					  && is_numeric($_POST['category'])
 					  && ($_POST['title'] != '') ;
					  
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		
		//get the record
		$q = "Select * From ". HONOURCATS_TABLE . " Where hc_division = " . $this->division;
		$result = $db->sql_query($q);
		$introMess = false;
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$introMess = true;
			}
			
			$this->toRender[] = new edit_honour_cat(false, $this->division, $row);
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new edit_honour_cat(true, $this->division);
	}

	
}


class edit_honour_entry
{
	var $id;
	var $category;
	var $item;
	var $rank;
	var $cat_items = '';
	
	var $add_record;

	function edit_honour_entry($add_record, $category, $cat_items, $rank, $item = 0)
	{
		if($add_record)
		{
			$this->add_record = true;
		}
		else
		{
			$this->add_record = false;
		}
		
		$this->category = $category;
		$this->item = $item;
		$this->rank = $rank;
		
		foreach($cat_items as $cat_item) 
		{
			$this->cat_items .= '<option value="' . $cat_item['i_index'] . '" ' . 
								($cat_item['i_index'] == $this->item ? 'SELECTED' : '') .
								'>' . $cat_item['i_title'] . '</option>';
		}

				
	}

	function render()
	{		
		global $skin_dir, $lang;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['EditButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'editRecord';
		$remS = $this->add_record ? '<!--' : '';
		$remE = $this->add_record ? '-->' : '';
		
		$rank = $this->rank;
		$cat_items = $this->cat_items;
		$category = $this->category;
			
		include $skin_dir . 'admin/edithonourentry.tpl';
	}
}


class honour_entry
{
	var $mess;
	var $category;
	var $toRender = Array();

	function honour_entry($category)
	{
		$mess = '';
		$this->category = $category;
		if(isset($_POST['removeRecord']) && isset($_POST['id']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
		$this->setupRecords();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		//get the record
		$qSel = "select hc_title from ". HONOURCATS_TABLE . " Where hc_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". HONOURENTRIES_TABLE . " Where he_cat = " . $_POST['itemID'] . " AND he_rank = " . $_POST['id'];
			
			$db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['hc_title'] . " " .  $lang['ResultsPos'] . " " .   $_POST['id'], 
								$lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". HONOURENTRIES_TABLE . " (he_cat, he_item, he_rank) values('" .
							 $_POST['itemID'] . "', " . $_POST['cat_items'] .", " . $_POST['id'] .')';
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', '' , $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "UPDATE  ". HONOURENTRIES_TABLE . " 
					 SET he_item = " . $_POST['cat_items'] . " 
					 WHERE he_cat = " . $_POST['itemID'] . " AND he_rank = " .  $_POST['id'];
			
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', '', $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
			
		}
		
		
		return $valid && is_numeric($_POST['itemID'])
 					  && is_numeric($_POST['id'])
 					  && is_numeric($_POST['cat_items']);
					  
	}
	
	function setupRecords()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		
		$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
		
		//get the category details
		$q = "Select * From ". HONOURCATS_TABLE . " Where hc_id = " . $this->category;
		$result = $db->sql_query($q);
		
		if($cat = $db->sql_fetchrow($result)) 
		{
			//get the cat items
			$cat_items= Array();
			
			//drivers
			if($cat['hc_cat'] == 0)
			{
				$q = "Select pf_drivername as i_title, user_id as i_index
				      From ". DRIVERSTANDINGS_TABLE . ", " . DRIVERNAME_TABLE . ", " . HONOURCATS_TABLE . "
					  Where hc_division = ct_div AND ct_id = user_id AND hc_id = " . $this->category . "
					  ORDER BY ct_pos ASC";
			}
			//teams
			else if($cat['hc_cat'] == 1)
			{
				$q = "Select t_name as i_title, t_id as i_index
				      From ". TEAMSTANDINGS_TABLE . ", " . TEAMS_TABLE . ", " . HONOURCATS_TABLE . "
					  Where hc_division = ct_div AND ct_id = t_id AND hc_id = " . $this->category . "
					  ORDER BY ct_pos ASC";
			}
			//manufacturers
			else
			{
				$q = "Select CONCAT(c_manufacturer, ' ', c_model) as i_title, c_id as i_index
				      From ". MANUFACTURERSTANDINGS_TABLE . ", " . CARS_TABLE . ", " . HONOURCATS_TABLE . "
					  Where hc_division = ct_div AND ct_id = c_id AND hc_id = " . $this->category . "
					  ORDER BY ct_pos ASC";
			}
			
			$result = $db->sql_query($q);
			while($row = $db->sql_fetchrow($result)) 
			{
				$cat_items[] = $row;
			}
		
			//get the records
			$q = "Select * From ". HONOURENTRIES_TABLE . " Where he_cat = " . $this->category;
			$result = $db->sql_query($q);
			
			$recs = Array();
			
			while($row = $db->sql_fetchrow($result)) 
			{
				$recs[$row['he_rank']] = $row;
			}
			
			for($x = 1; $x <= 3; $x++)
			{
				if(array_key_exists($x, $recs))
				{
					$this->toRender[] = new edit_honour_entry(false, $this->category, $cat_items, $x, $recs[$x]['he_item']);
				}
				else
				{
					$this->toRender[] = new edit_honour_entry(true, $this->category, $cat_items, $x);
				}
			}
		}
	}
}
?>